<?php
// +----------------------------------------------------------------------
// | yylAdmin 前后分离，简单轻量，免费开源，开箱即用，极简后台管理系统
// +----------------------------------------------------------------------
// | Copyright https://gitee.com/skyselang All rights reserved
// +----------------------------------------------------------------------
// | Gitee: https://gitee.com/skyselang/yylAdmin
// +----------------------------------------------------------------------

// 登录退出控制器
namespace app\admin\controller\admin;

use think\facade\Request;
use app\common\validate\admin\UserValidate;
use app\common\service\admin\LoginService;
use app\common\service\admin\SettingService;
use app\common\utils\CaptchaUtils;
use hg\apidoc\annotation as Apidoc;


class Login
{
    /**
     * @api {get}  admin/admin.Login/captcha 验证码
     * @apiVersion 0.1.0
     * @apiName verify
     * @apiGroup 后台登录接口
     *
     *
     * @apiSuccess {String} firstname Firstname of the User.
     * @apiSuccess {String} lastname  Lastname of the User.
     * @apiSampleRequest admin/admin.Login/captcha
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "code": 200,
     *       "msg": "操作成功",
     *       "data": {
     *       "verify_switch": true,
     *       "verify_id": "507bd1f88b9dc72ff9756e8a6cfb2013",
     *       "verify_src": ""
     *            }
     *       }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function captcha()
    {
        $setting = SettingService::captchaInfo();
        if ($setting['captcha_switch']) {
            $data = CaptchaUtils::create();
        } else {
            $data['captcha_switch'] = $setting['captcha_switch'];
        }

        return success($data);
    }

    /**
     * @api {get}  admin/admin.Login/login 登录
     * @apiVersion 0.1.0
     * @apiName login
     * @apiGroup 后台登录接口
     *
     * @apiParam {string} username 用户名称.
     * @apiParam {string} password 密码.
     *
     * @apiSuccess {String} admin_admin_id 用户id.
     * @apiSuccess {String} admin_token  用户token.
     * @apiSampleRequest admin/admin.Login/login
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           code: 200,
     *           msg: "登录成功",
     *           data: {
     *           admin_admin_id: 2,
     *           admin_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsampBZG1pbiIsImlhdCI6MTYyOTE2Nzg2OCwibmJmIjoxNjI5MTY3ODY4LCJleHAiOjE2MjkyMTEwNjgsImRhdGEiOnsiYWRtaW5fYWRtaW5faWQiOjIsImxvZ2luX3RpbWUiOiIyMDIxLTA4LTE3IDEwOjM3OjQ4IiwibG9naW5faXAiOiIxMjcuMC4wLjEifX0.QhnW5Y-3QiQ1tJBkpIHkhIcBr6JplHaI_byvZxN9k8E"
     *               }
     *           }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function login()
    {
        $param['username']     = Request::param('username/s', '');
        $param['password']     = Request::param('password/s', '');
        $param['captcha_id']   = Request::param('captcha_id/s', '');
        $param['captcha_code'] = Request::param('captcha_code/s', '');

        validate(UserValidate::class)->scene('login')->check($param);

        $setting = SettingService::captchaInfo();
        if ($setting['captcha_switch']) {
            if (empty($param['captcha_code'])) {
                exception('请输入验证码');
            }
            $check = CaptchaUtils::check($param['captcha_id'], $param['captcha_code']);
            if (empty($check)) {
                exception('验证码错误');
            }
        }

        $data = LoginService::login($param);

        return success($data, '登录成功');
    }


    public function logout()
    {
        $param['admin_user_id'] = admin_user_id();

        validate(UserValidate::class)->scene('id')->check($param);

        $data = LoginService::logout($param['admin_user_id']);

        return success($data, '退出成功');
    }
}
